package com.product.system.utils;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Element;
import com.itextpdf.text.pdf.*;

import java.io.FileOutputStream;

/**
 * @author 卟哭！
 * @date 2023/12/25 14:53
 * @Description: 对指定PDF文件插入自定义水印
 * @Version 1.0
 */
public class WaterMarkUtil {
    /**
     * pdf生成水印
     *
     * @param srcPdfPath       插入前的文件路径
     * @param tarPdfPath       插入后的文件路径
     * @param WaterMarkContent 水印文案
     * @param numberOfPage     每页需要插入的条数
     * @throws Exception
     */
    public static void addWaterMark(String srcPdfPath, String tarPdfPath, String WaterMarkContent, int numberOfPage) throws Exception {
        PdfReader reader = new PdfReader(srcPdfPath);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(tarPdfPath));
        PdfGState gs = new PdfGState();
        System.out.println("adksjskalfklsdk");
        //设置字体
        String osName = System.getProperty("os.name");
        String url = "";
        if (osName.toLowerCase().contains("window")) {
            url = "d:/";
        }else {
            url = "/opt/";
        }
        //BaseFont font = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        BaseFont font = BaseFont.createFont(url+"pdf/font/STHUPO.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        //BaseFont font = BaseFont.createFont("D:\\pdf\\font\\STHUPO.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

        // 设置透明度
        gs.setFillOpacity(0.2f);

        int total = reader.getNumberOfPages() + 1;
        PdfContentByte content;
        for (int i = 1; i < total; i++) {
            content = stamper.getOverContent(i);
            content.beginText();
            content.setGState(gs);
            //水印颜色
            content.setColorFill(BaseColor.DARK_GRAY);
            //水印字体样式和大小
            content.setFontAndSize(font, 35);
            //插入水印  循环每页插入的条数
            for (int j = 0; j < numberOfPage; j++) {
                content.showTextAligned(Element.ALIGN_CENTER, WaterMarkContent, 300, 200 * (j + 1), 30);
            }
            content.endText();
        }
        stamper.close();
        reader.close();
        //boolean b = deleteFile(srcPdfPath);
        System.out.println("PDF水印添加完成！");
    }



}


